// pages/login/login.js
const app = getApp(); -
Page({
    data: {
        username: "",
        password: "",
        checked: true,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        list: [{
            name: "scale-up"
        }]
    },
    // 单选框
    checked: function (e) {
        var check = this.data.checked;
        if (check) {
            this.data.checked = false;
        } else {
            this.data.checked = true;
        }
        this.setData({
            "checked": this.data.checked,
        })
    },
    // 获取用户信息
    handleGetUserInfo(e) {
        const {
            userInfo
        } = e.detail;
        wx.setStorageSync('userinfo', userInfo);
    },
    // 账号和密码
    getUserName: function (e) {
        let value = e.detail.value;
        this.setData({
            username: value,
        })
        // wx.setStorageSync('username', value);
    },
    getPassword: function (e) {
        let value = e.detail.value;
        this.setData({
            password: value,
        })
        // wx.setStorageSync('password', value);
    },
    onLoad: function (options) {
        // // 验证是否为第一次登录
        if (wx.getStorageSync('userId')) {
            // console.log("已有数据");
            wx.switchTab({
                url: '/pages/index/index'
            })
        }
    },
    // 发送请求
    doLogin: function (e) {
        var that = this;
        // 按钮动画
        var anmiaton = e.currentTarget.dataset.class;
        that.setData({
            animation: anmiaton
        })
        setTimeout(function () {
            that.setData({
                animation: ''
            })
        }, 1000)
        // 检验非空
        if (that.data.checked == false) {
            wx.showToast({
                icon: 'none',
                title: '请勾选用户协议',
            })
        } else if (that.data.username.length == 0 || that.data.password.length == 0) {
            wx.showToast({
                icon: 'none',
                title: '用户名或密码为空!',
                duration: 2000,
            })
        } else {
            wx.request({
                url: 'http://172.17.60.141/Login',
                method: 'GET',
                header: {
                    'Content-Type': 'application/json'
                },
                data: {
                    user: that.data.username,
                    password: that.data.password,
                },
                success: function (res) {
                    // console.log(res.statusCode);
                    if (res.statusCode == 200) {
                        // console.log("获取成功");
                        wx.setStorageSync('userId', res.data.userID);
                        wx.showToast({
                            icon: 'none',
                            title: "登陆成功,第一次登录可能较慢，请耐心等待",
                        })
                        wx.switchTab({
                            url: '../index/index',
                        })
                    } else {
                        wx.showToast({
                            icon: 'none',
                            title: "用户名密码错误",
                        })
                    }
                }
            })
        }
    }
})